package com.sh.suhe.utildemos.views.activity.photo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sh.suhe.utildemos.R;
import com.sh.suhe.utildemos.dialogUtils.RightDialog;
import com.sh.suhe.utildemos.model.PaPhotoModel;
import com.sh.suhe.utildemos.model.PaPhotoTypeModel;
import com.sh.suhe.utildemos.utils.JsonUtil;
import com.sh.suhe.utildemos.utils.LogUtil;
import com.sh.suhe.utildemos.utils.ToastUtils;
import com.sh.suhe.utildemos.utils.http.NetUtil;
import com.sh.suhe.utildemos.views.activity.photo.util.BaiduUtils;
import com.sh.suhe.utildemos.views.activity.photo.util.MeiTuUtils;
import com.sh.suhe.utildemos.views.activity.photo.util.SoGouUtils;
import com.sh.suhe.utildemos.views.adapter.PaPhotoAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PaPhotoActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG= PaPhotoActivity.class.getSimpleName();

    List<PaPhotoTypeModel> types = new ArrayList<>();
    List<PaPhotoModel> imgList = new ArrayList<>();
    TextView typeTV;
    TextView nextTv, searchBut;
    EditText searchEt;
    RecyclerView imgListView;
    PaPhotoAdapter adapter;
    RightDialog rightDialog;
    String nextPage = "";
    public static int type = 2 ;
    private String search = "壁纸";
    private String tag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pa_photo);

        type =getIntent().getIntExtra("type",1);

        typeTV = findViewById(R.id.pa_photo_tv);
        typeTV.setOnClickListener(this);
        nextTv = findViewById(R.id.pa_photo_next);
        nextTv.setOnClickListener(this);
        searchEt = findViewById(R.id.pa_photo_search);
        searchBut = findViewById(R.id.pa_photo_to_search);
        searchBut.setOnClickListener(this);

        imgListView = findViewById(R.id.pa_photo_list);

//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        int space = 10;
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //解决item跳动
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        imgListView.setLayoutManager(layoutManager);
        imgListView.setItemAnimator(null);
        imgListView.addItemDecoration(new StaggeredItemDecoration(space));//单位px

        adapter = new PaPhotoAdapter(this);
        imgListView.setAdapter(adapter);

        rightDialog = new RightDialog(this);
        rightDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog
        rightDialog.setTypeClick(typeClick);

        adapter.setImageClick(new PaPhotoAdapter.ImageClick() {
            @Override
            public void clickImg(String url, int position) {
                Intent intent = new Intent();
                intent.setClass(PaPhotoActivity.this, PaPhotoDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",imgList.get(position).getDetailUrl());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        searchEt.setText(search);
        loadImg();
//        OKHttpRequest a;
    }

    private void loadImg() {
        if (type == 1) {
            loadUrl(MeiTuUtils.url);
        }else if (type == 2) {
            loadSoGou(search, tag);
        }else if (type == 3) {
            loadBaiDu(search, tag);
        }
    }

    RightDialog.TypeClick typeClick = new RightDialog.TypeClick() {
        @Override
        public void clickType(PaPhotoTypeModel typeModel) {
            if (type == 1) {
                loadUrl(typeModel.getUrl());
            }else if (type ==2) {
                String tag = typeModel.getType()+ "," + typeModel.getUrl();
                loadSoGou(search, tag);
            }
            if (rightDialog != null){
                rightDialog.dismiss();
            }
        }
    };
    private void loadUrl(String url) {
        MeiTuUtils.loadUrl(url, (imgList, types, nextPage) -> {
            runOnUiThread(() ->{
                adapter.setImgList(imgList);
                adapter.notifyDataSetChanged();

                rightDialog.setData(types);

                this.imgList = imgList;
                this.types = types;
                this.nextPage = nextPage;
            });
        });
    }
    private void loadSoGou(String query, String tag) {
        SoGouUtils.url(query, tag, (imgList, types, nextPage) -> {
            runOnUiThread(() ->{
                adapter.setImgList(imgList);
                rightDialog.setData(types);

                this.imgList = imgList;
                this.types = types;
                this.nextPage = nextPage;
            });
        });
    }
    private void loadBaiDu(String query, String tag){
        BaiduUtils.url(query, tag, (imgList, types, nextPage) -> {
            runOnUiThread(() ->{
                adapter.setImgList(imgList);
                adapter.notifyDataSetChanged();

                rightDialog.setData(types);

                this.imgList = imgList;
                this.types = types;
                this.nextPage = nextPage;
            });
        });
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.pa_photo_next){
            if (TextUtils.isEmpty(nextPage)){
                ToastUtils.showToast("已经是最后一页");
                return;
            }
            loadUrl(nextPage);
        }else if (v.getId() == R.id.pa_photo_tv) {
            if (!rightDialog.isShowing()) {
                rightDialog.show();
            }
        }else if (v.getId() == R.id.pa_photo_to_search) {

            if (TextUtils.isEmpty(searchEt.getText().toString())) {
                ToastUtils.showToast("内容不能为空");
                return;
            }
            search = searchEt.getText().toString();
            this.tag = "";
            loadImg();

        }
    }

    public interface LoadPhoto {
        void loadBack(List<PaPhotoModel> imgList, List<PaPhotoTypeModel> types, String nextPage);
    }
}